/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.plugins.impl;

import java.util.List;

import org.grayrabbit.cms.dao.plugins.SurveySubjectDao;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.SurveySubject;
import org.grayrabbit.cms.service.plugins.SurveySubjectService;
import org.grayrabbit.cms.vo.plugins.SurveySubjectBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 16:08:41  
 */
@Service
@Transactional
public class SurveySubjectServiceImpl extends GenericServiceImpl implements SurveySubjectService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("surveySubjectDaoImpl")
	private SurveySubjectDao surveySubjectDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see SurveySubjectService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public SurveySubject getSurveySubject(final String subjectId){
		SurveySubject surveySubject = surveySubjectDao.findById(subjectId);
		surveySubjectDao.initialize(surveySubject);
		return surveySubject;
	}
	
	/**
	 * @see SurveySubjectService#delete(String)
	 */
	@Transactional
	public void delete(final SurveySubject surveySubject) {
		surveySubjectDao.makeTransient(surveySubject);
	}
	
	/**
	 * @see SurveySubjectService#save(OperLog)
	 */
	@Transactional
	public void save(final SurveySubject surveySubject) {
		surveySubjectDao.makePersistent(surveySubject);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see SurveySubjectService#getPage(SurveySubjectBean)
	 */
	@Transactional(readOnly=true)
	public Page<SurveySubject> getPage(final SurveySubjectBean surveySubjectBean){
		Page<SurveySubject> page =  new Page<SurveySubject>();
		page.setTotal(surveySubjectDao.count(surveySubjectBean));
		page.setResult(surveySubjectDao.findByQc(surveySubjectDao.assembly(surveySubjectBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see SurveySubjectService#list(Page, SurveySubjectBean)
	 */
	public void list(Page<SurveySubject> page, SurveySubjectBean surveySubjectBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(surveySubjectDao.count(surveySubjectBean));
		}
		page.setResult(surveySubjectDao.findByPage(page,surveySubjectBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see SurveySubjectService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			surveySubjectDao.makeTransient(surveySubjectDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				surveySubjectDao.clear();
		}
		
	}
	/**
	 * 根据surveyId查询SurveySubject列表
	 * @param voteId
	 * @return
	 */
	public List<SurveySubject> findBySurveyId(String surveyId){
		
		return surveySubjectDao.findBySurveyId(surveyId);
	}
	/**
	 * 根据id查找记录列表
	 * @param id
	 * @return
	 */
	public List<SurveySubject> findBySubId(String id){
		return surveySubjectDao.findBySubId(id);
	}
}